<template>
  <div class="app-container">
    <el-form
      :model="stationQuery"
      ref="stationQuery"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="油站" prop="stationName">
        <el-select
          v-model="stationQuery.stationId"
          placeholder="油站"
          clearable
          style="width: 350px"
          size="small"
        >
          <el-option :value="undefined" selected>全部</el-option>
          <el-option
            :value="ele.stationId"
            v-for="ele in stationList"
            :key="ele.oilId"
            :label="ele.stationName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createdDate">
        <el-date-picker
          size="small"
          style="width: 350px"
          v-model="dateRangeCreatedDate"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-card class="card-table">
      <el-row :gutter="10" class="mb8">
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="handleQuery"
        ></right-toolbar>
      </el-row>
      <el-table
        :data="stationList"
        style="width: 100%"
        v-loading="stationLoading"
      >
        <af-table-column prop="rechargeDay" label="日期" align="left">
        </af-table-column>
        <af-table-column prop="stationName" label="油站名称" align="left">
        </af-table-column>
        <af-table-column prop="oilName" label="油品" align="left">
        </af-table-column>
        <af-table-column prop="oilOfficialPrice" label="挂牌价" align="left">
        </af-table-column>
        <af-table-column prop="rechargeAmt" label="充值金额" align="left">
        </af-table-column>
        <af-table-column prop="orderLiters" label="升数" align="left">
        </af-table-column>
        <af-table-column prop="receivableAmt" label="原价" align="left">
        </af-table-column>
        <af-table-column
          prop="oilStationSettlementPriceSum"
          label="平台支付价"
          align="left"
        >
        </af-table-column>
        <af-table-column prop="cardDiscount" label="卡优惠" align="left">
        </af-table-column>
        <af-table-column
          prop="oilStationSettlementPrice"
          label="油卡价格"
          align="left"
        >
        </af-table-column>
        <af-table-column prop="balance" label="油卡余额" align="left">
        </af-table-column>
        <af-table-column prop="discount" label="让利" align="left">
        </af-table-column>
        <af-table-column prop="discountSum" label="让利金额" align="left">
        </af-table-column>
        <af-table-column prop="amt" label="回款金额" align="left">
        </af-table-column>
      </el-table>
    </el-card>
    <pagination
      :hidden="stationListTotal > 0 ? false : true"
      :total="stationListTotal"
      :page.sync="stationQuery.pageNum"
      :limit.sync="stationQuery.pageSize"
      @pagination="getRechargeList"
      :autoScroll="true"
    />
  </div>
</template>

<script>
import {
  listDept,
  getDept,
  delDept,
  addDept,
  updateDept,
  listDeptExcludeChild,
  listTree,
  setMnoSplit,
} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { regionData } from "element-china-area-data";
import { getToken } from "@/utils/auth";
import {
  addStation,
  upStation,
  getRechargeList,
  exportRecharge,
  getStationList,
  addPayWay,
  upPayWay,
  getPayWayList,
  addOilTank,
  upOilTank,
  getOilTankList,
  getPayWayListByStationId,
  upPayWayListByStationId,
  getOilList,
  addOilAndGun,
  upOilAndGun,
  getOilAndGunList,
  delOilTank,
  delOilAndGun,
  delStation,
  addRecharge,
} from "@/api/dept";
import translateAddress from "@/utils/translateAddress.js";
// import addRecharge from "@/station/recharge.js";

export default {
  name: "Dept",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 是否显示弹出层
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      stationList: [],
      stationListTotal: 0,
      stationLoading: false,
      stationQuery: {
        pageSize: 10,
        pageNum: 1,
        stationName: "",
        status: "",
      },
    };
  },
  created() {
    this.getRechargeList();
    this.getStationList();
  },
  methods: {
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getRechargeList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("stationQuery");
      this.handleQuery();
    },
    getRechargeList() {
      this.stationLoading = true;
      getRechargeList(
        this.addDateRange(this.stationQuery, this.dateRangeCreatedDate)
      )
        .then((res) => {
          this.stationListTotal = res.total;
          this.stationList = res.rows;
        })
        .finally(() => (this.stationLoading = false));
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.stationQuery;
      this.$confirm("是否确认导出报表?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportRecharge(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    getOilAndGunList() {
      this.oilAndGunQuery.stationId = this.currentStation.stationId;
      getOilAndGunList(this.oilAndGunQuery).then((res) => {
        console.log(res);
        if (res.rows == null) {
          this.oilAndGunList = [];
          this.oilAndGunListTotal = 0;
        }
        this.oilAndGunList = res.rows;
        this.oilAndGunListTotal = res.total;
        console.log(this.oilAndGunList);
      });
    },
    getStationList() {
      getStationList(this.stationQuery).then((res) => {
        this.stationList = res.rows;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.deptId != undefined) {
          } else {
          }
        }
      });
    },
    stationSubmitForm: function () {
      this.$refs["stationForm"].validate((valid) => {
        if (valid) {
          if (this.stationForm.stationId != undefined) {
            upStation(this.stationForm).then((res) => {
              this.msgSuccess("更新成功");
              this.getRechargeList();
              this.stationOpen = false;
            });
          } else {
            addStation(this.stationForm).then((res) => {
              this.msgSuccess("新增成功");
              this.getRechargeList();
              this.stationOpen = false;
            });
          }
        }
      });
    },
    rechargeSubmitForm: function () {
      addRecharge(this.rechargeForm).then((res) => {
        this.msgSuccess("充值成功");
        this.getRechargeList();
        this.rechargeOpen = false;
      });
    },
    payWaySubmitForm: function () {
      this.payWayForm.stationId = this.currentStation.stationId;
      this.$refs["payWayForm"].validate((valid) => {
        if (valid) {
          upPayWayListByStationId(this.payWayForm).then((res) => {
            this.msgSuccess("更新成功");
            this.payWayOpen = false;
          });
        }
      });
    },

    oilAndGunSubmitForm: function () {
      this.oilAndGunForm.stationId = this.currentStation.stationId;
      this.oilAndGunForm.gunNameList.sort((q, w) => q - w);
      this.$refs["oilAndGunForm"].validate((valid) => {
        if (valid) {
          if (this.oilAndGunForm.gunId != undefined) {
            upOilAndGun(this.oilAndGunForm).then((res) => {
              this.getOilAndGunList();
              this.msgSuccess("更新成功");
              this.oilAndGunConfOpen = false;
            });
          } else {
            addOilAndGun(this.oilAndGunForm).then((res) => {
              this.getOilAndGunList();
              this.msgSuccess("新增成功");
              this.oilAndGunConfOpen = false;
            });
          }
        }
      });
    },
    oilTankSubmitForm: function () {
      this.$refs["oilTankConfForm"].validate((valid) => {
        if (valid) {
          if (this.oilTankConfForm.tankId != undefined) {
            upOilTank(this.oilTankConfForm).then((res) => {
              this.msgSuccess("更新油罐成功");
              this.getOilTankList();
              this.oilTankConfOpen = false;
            });
          } else {
            addOilTank(this.oilTankConfForm).then((res) => {
              this.msgSuccess("新增油罐成功");
              this.getOilTankList();
              this.oilTankConfOpen = false;
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss">
.avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}

.card-table {
  margin-top: 40px;
  background-color: #f8f8f8;
  .el-button {
    margin-bottom: 20px;
    margin-right: 20px;
  }
}
::v-deep .el-form-item {
  .el-form-item__label {
    padding: 0;
    margin: 0;
  }
  .el-button--cyan {
    margin-left: 20px;
  }
}
</style>
